StaffView = Ext.extend(Ext.Panel, {
	constructor : function(a) {
		Ext.applyIf(this, a);
		this.initUIComponents();
		StaffView.superclass.constructor.call(this, {
			id : "StaffView",
			title : "员工信息",
			iconCls : "menu-AccountManagement",
			layout : "border",
			autoScroll : true
		});
	},
	initUIComponents : function() {
		this.initSearchPanel();
		this.initGridPanel();
		this.items = [ this.searchPanel, this.gridPanel ];
	},
	onSearch : function(c) {
		var a = Ext.getCmp("StaffSearchForm");
		var b = Ext.getCmp("StaffGrid");
		if (a.getForm().isValid()) {
			$search({
				searchPanel : a,
				gridPanel : b
			});
		}
	}
});
StaffView.prototype.initSearchPanel = function() {
	this.searchPanel = new Ext.FormPanel({
		region : "north",
		height : 35,
		frame : false,
		border : false,
		id : "StaffSearchForm",
		layout : "hbox",
		layoutConfig : {
			padding : "5",
			align : "middle"
		},
		defaults : {
			xtype : "label",
			border : false,
			margins : {
				top : 0,
				right : 4,
				bottom : 4,
				left : 4
			}
		},
		items : [ {
			text : "工号"
		}, {
			xtype : "textfield",
			name : "Q_cardNo_S_LK"
		},{
			text : "姓名"
		}, {
			xtype : "textfield",
			name : "Q_name_S_LK"
		}, {
			xtype : "button",
			text : "查询",
			iconCls : "search",
			scope : this,
			handler : this.onSearch.createCallback(this)
		}, {
			xtype : "button",
			text : "清空",
			scope : this,
			iconCls : "reset",
			handler : Appreset
		} ]
	});
};
StaffView.prototype.initGridPanel = function() {
	this.toolbar = new Ext.Toolbar({
		height : 30,
		items : []
	});
	if (isGranted("_StaffAdd")) {
		this.toolbar.add(new Ext.Button({
			text : "添加员工",
			iconCls : "add-user",
			handler : function() {
				new StaffForm({
					staffId : null
				}).show();
			}
		}));
	}
	if (isGranted("_StaffDel")) {
		this.toolbar.add(new Ext.Button({
			iconCls : "btn-delete",
			text : "删除员工",
			handler : function() {
				var h = Ext.getCmp("StaffGrid");
				var d = h.getSelectionModel().getSelections();
				if (d.length == 0) {
					Ext.ux.Toast.msg("信息", "请选择要删除的记录！");
					return;
				}
				var ids = "";
				for (var i = 0; i < d.length; i++) {
					if (d[i].data.id == 1) {
						Ext.ux.Toast.msg("超级管理员不能被删除！");
						return;
					} else {
						ids += d[i].data.id
						if (i < d.length - 1) {
							ids = ids + ",";
						}
					}
				}
				if (ids) {
					StaffView.remove(ids);
				}
			}
		}));
	}
	var b = new Ext.data.Store({
		proxy : new Ext.data.HttpProxy({
			url : __ctxPath + "/system/listStaff.do"
		}),
		reader : new Ext.data.JsonReader({
			root : "result",
			totalProperty : "totalCounts",
			fields : [ {
				name : "id",
				type : "int"
			}, "name", "phone", "address","cardNo","status","email","accessionTime"/*,"department"*/]
		}),
		remoteSort : true
	});
	b.setDefaultSort("id", "desc");
	b.load({
		params : {
			start : 0,
			limit : 25
		}
	});
	var e = new Ext.grid.CheckboxSelectionModel();
	var a = new Ext.grid.ColumnModel(
			{
				columns : [
						e,
						new Ext.grid.RowNumberer(),
						{
							header : "staffId",
							dataIndex : "id",
							hidden : true
						},
						{
							header : "状态",
							dataIndex : "status",
							width : 30,
							renderer : function(f) {
								var g = "";
								if (f == "1") {
									g += '<img title="激活" src="'+ __ctxPath+ '/resources/images/flag/customer/effective.png"/>';
								} else {
									g += '<img title="禁用" src="'+ __ctxPath+ '/resources/images/flag/customer/invalid.png"/>';
								}
								return g;
							}
						},
						{
							header : "工号",
							dataIndex : "cardNo",
							width : 60
						},
						{
							header : "姓名",
							dataIndex : "name",
							width : 60
						},
						/*{
							header : "所属部门",
							dataIndex : "department",
							renderer : function(f) {
								if (f == null) {
									return "";
								} else {
									return f.depName;
								}
							},
							width : 60
						},*/
						{
							header : "地址",
							dataIndex : "address"
						},
						{
							header : "邮箱",
							dataIndex : "email",
							width : 120
						},
						{
							header : "入职时间",
							dataIndex : "accessionTime",
							width : 100
						},
							 
						{
							header : "管理",
							dataIndex : "id",
							sortable : false,
							width : 60,
							renderer : function(j, i, g, m, h) {
								var l = g.data.id;
								var f = g.data.name;
								var k = "";
								if (l != 1) {
									if (isGranted("_StaffDel")) {
										k += '<button title="删除" value=" " class="btn-delete" onclick="StaffView.remove('
												+ l + ')"></button>';
									}
									if (isGranted("_StaffEdit")) {
										k += '&nbsp;<button title="编辑" value=" " class="btn-edit" onclick="StaffView.edit('
												+ l + ')"></button>';
									}
								}
								return k;
							}
						} ],
				defaults : {
					sortable : true,
					menuDisabled : true,
					width : 100
				}
			});
	this.gridPanel = new Ext.grid.GridPanel({
		id : "StaffGrid",
		tbar : this.toolbar,
		store : b,
		region : "center",
		shim : true,
		trackMouseOver : true,
		disableSelection : false,
		loadMask : true,
		cm : a,
		sm : e,
		viewConfig : {
			forceFit : true,
			enableRowBody : false,
			showPreview : false
		},
		bbar : new HT.PagingBar({
			store : b
		})
	});
	// this.gridPanel.addListener("rowdblclick", d);
	var c = this.gridPanel;
	function d(g, f, h) {
		g.getSelectionModel().each(function(j) {
			var i = j.data.staffId;
			if (isGranted("_StaffEdit") && i != 1) {
				StaffView.edit(i, j.data.username);
			}
		});
	}
};
StaffView.remove = function(a) {
	Ext.Msg.confirm("删除操作", "你确定要删除该人员吗?", function(b) {
		if (b == "yes") {
			Ext.Ajax.request({
				url : __ctxPath + "/system/deleteStaff.do",
				method : "post",
				params : {
					ids : a
				},
				success : function(d) {
					console.log(d);
					var c = Ext.util.JSON.decode(d.responseText);
					if (c.msg==null) {
						Ext.ux.Toast.msg("操作提示", "人员删除成功");
					} else {
						Ext.ux.Toast.msg("操作提示", c.msg);
					}
					Ext.getCmp("StaffGrid").getStore().reload();
				},
				failure : function() {
					Ext.ux.Toast.msg("操作提示", "人员删除失败");
				}
			});
		}
	});
};

Appreset = function() {
	var a = Ext.getCmp("StaffSearchForm");
	a.getForm().reset();
}
StaffView.edit = function(id) {
	new StaffForm({
		staffId : id
	}).show();
};
